﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using Bintje;
using Bintje.bintje;

public partial class UI_trail_details : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //

            if (Request["TrailID"] != null)
            {
                hlShareTrail.NavigateUrl = "~/UI/TrailShare.aspx?TrailID=" + Request["TrailId"].ToString();

                hprViewMap.NavigateUrl = "~/UI/Trail.aspx?TrailID=" + Request["TrailID"].ToString();
                int tid = 0;
                int.TryParse(Request["TrailID"].ToString(), out tid);
                if (tid != 0)
                {
                    GMap1.TrailID = tid;
                    Bintje.Trail trail = GMap1.LoadTrail();

                    FillControls(trail);
      
                    ctrl_Gallery.TrailID = tid;
                    ctrl_Gallery.Trail = trail;
                    ctrl_Trail.TrailID = tid;
                    ctrl_Trail.Trail = trail;
                    ctrl_Trail.ViewTrail = true;
                    ctrl_Trail.PhotoCount = 8;

                    showComments(trail);              
                }
            }

            if (Request["tmsg"] != null)
            {
                lblMsg.Text = "Comments saved successfully";
            }
            else
            {
                lblMsg.Text = "";
            }
        }
    }

    private void updateTrailVisitCount() 
    {
        // TO DO: Delete if no visit counts will be supported 
    }

    public void showComments(Bintje.Trail trail)
    {
        int tid = 0;
        int.TryParse(Request["TrailID"].ToString(), out tid);
        if (tid != 0)
        {
            if (null != trail && trail.Annotations != null)
            {
                IList<Annotation> tc = trail.Annotations;
                rptrTrailComments.DataSource = tc;
                rptrTrailComments.DataBind();

                if (tc.Count == 0)
                    lblComments.Text = "no comments available";
            }
            
        }
    }
    protected void trailcomment_databound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            LinkButton lnkDelete = (LinkButton)e.Item.FindControl("lnkDelete");
            int tid = 0;
            int.TryParse(Request["TrailID"].ToString(), out tid);
            
            imageoSession session = HttpContext.Current.Session["session"] as imageoSession;
            if (null != session)
            {
                IList<Bintje.Trail> myTrails = Bintje.Trail.Load(session);
                if (null != myTrails && myTrails.Count(trl => trl.Id == tid) > 0)
                {
                    lnkDelete.Visible = true;
                }
            }
        }
    }

    protected void btnDelete_click(object sender, EventArgs e)
    {
        int commentid =0;
        int.TryParse(((LinkButton)sender).CommandArgument, out commentid);
        if (commentid > 0)
        {
            int tid = 0;
            int.TryParse(Request["TrailID"].ToString(), out tid);
            if (tid != 0)
            {
                Bintje.Trail trail = Bintje.Trail.Get(tid);
                if (null != trail && trail.Annotations != null)
                {
                    IList<Annotation> tc = trail.Annotations;
                    Annotation comment = tc.Single(c => c.Id == commentid);
                    if (null != comment)
                    {
                        trail.Annotations.Remove(comment);
                        trail = trail.KeepImages(trail);
                        trail.Update();
                        tc = trail.Annotations;
                        rptrTrailComments.DataSource = tc;
                        rptrTrailComments.DataBind();

                        if (tc.Count == 0)
                            lblComments.Text = "no comments available";
                    }
                }
            }
        }
    }

    public void FillControls(Bintje.Trail trail)
    {
        if (Request["TrailID"] != null)
        {
            hprComments.NavigateUrl = string.Format("~/UI/add-trailComments.aspx?TrailId={0}", Request["TrailID"]);
            if (trail!= null)
            {
                ltrTrailTitle.Text = trail.Title;
                if (trail.UserId == AppUser.currUser())
                {
                    hlAddPhotos.NavigateUrl += "?TrailID=" + trail.Id;
                    hlAddPhotos.Visible = true;
                }

                ltrTrailName.Text = trail.Title;
                if (trail.Waypoints != null && trail.Waypoints.Count > 0)
                {
                    lblLOcation.Text = !string.IsNullOrEmpty(trail.Waypoints[0].City) ? trail.Waypoints[0].City : "not Available";
                    lblWaypointsCount.Text = trail.Waypoints.Count.ToString();
                }

                lblCoordinates.Text = trail.Latitude + ", " + trail.Longitude;
                lblLength.Text = "not Available"; 
                lblUploaded.Text = "";
                lblUploaded.NavigateUrl = string.Format("~/UI/userDetail.aspx?uid={0}", trail.Id);

                if (!string.IsNullOrEmpty(trail.Description))
                {
                    lblTrailDescription.Text = trail.Description;
                }
                else
                {
                    lblTrailDescription.Text = "No Trail Description Available at this time";
                }

                if (null != trail.Layers && trail.Layers.Count > 0) 
                {
                    for (int i = 0; i < trail.Layers.Count; i++)
                    {
                        lblType.Text += trail.Layers[i].LayerName + ",";
                    }
                    lblType.Text.TrimEnd(new char[] { ',' });
                }
            }
        }
    }

    protected void rptrTrails_DataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Literal ltr = (Literal)e.Item.FindControl("ltrDataAdded");
            if (ltr != null)
            {
                ltr.Text = (DateTime.Parse(ltr.Text)).ToShortDateString();
            }


        }
    }
}
